

var jcrop_api; 
var linkto; 
var originalheight3;
var originalwidth3;
var originalwidth2;
var originalheight2;
var baseheight;
var basewidth;
var callbackset;
var callbackset2;
var imgmidset;
var toggledata=1;
function togglesort()
{
if(toggledata)
{
 toggledata=0;
 $('#addform .featureclass').addClass('repoSize');
 $('#addform').sortable(
 {
    cursor: 'move',
    start:function (event,ui) {
      
    },
    stop: function(event, ui) { 
    
       //var elem = "#" + ui.item.context.id + " textarea";
        if($('.texteditorsetform textarea.inpAddCtntNoBd').length)
        {
         $.each($('.texteditorsetform textarea.inpAddCtntNoBd'), function(index ) {
        var valuedata2= $(this).sceditor('instance').val();
        
        $(this).sceditor('instance').destroy();
        $(this).sceditor({
plugins: "bbcode",
toolbar: "bold,italic,underline|left,center,right|bulletlist,orderedlist,link",
style: "/js/vendor/minified/jquery.sceditor.default.min.css",
emoticonsRoot :'/'
});

$(this).sceditor('instance').insert(valuedata2);



         });

        }
        
        
    } 
}
 
 );
 $( '#addform' ).sortable('enable');  
}else
{
toggledata=1;
 $('#addform .featureclass').removeClass('repoSize');
 $( '#addform' ).sortable('disable');
}
  

} 
var passboxset=0;
function getpopupboxresize(width,height,link,originalwidth,originalheight,callback,imgmid,passbox)
{
callbackset=callback;
imgmidset=imgmid;
basewidth=width;
baseheight=height;
if(passbox)
{
passboxset=1;

}else
{
passboxset=0;
}
var windowheight=$(window).height()-450;
var windowwidth=$(window).width()-450;
 originalheight2=parseInt(originalheight,10);
 originalwidth2=parseInt(originalwidth,10);
 originalwidth3=originalwidth2;
 originalheight3=originalheight2;
linkto=link;

if(windowheight<originalheight3)
{

originalwidth3=parseInt((windowheight*originalwidth3)/originalheight3,10);
originalheight3=windowheight;


}


//var code = '<div style="padding:10px;background:#fff" id="inline_content">\n';
//code += '<p><strong>Resize And Crop</strong></p>\n';
//code += '<p>Crop your Image</p>\n';
//code += '<p style="text-align:center"><img class="cboxPhoto" src="'+link+'" width="'+originalwidth3+'" height="'+originalheight3+'" ></p>\n';
//code += '<p><input type="button" value="OK" onclick="updateCoords()" ><input type="button" value="Cancel" onclick="$.colorbox.close();" ></p>\n';
//code += '\n';
//code += '</div>';



var code = '<div class="popGen posPop">\n';
code += '<div class="topGen clBd2 bdBtm">\n';
code += '<div class="wrpTtlGen">\n';
code += '<h1 class="ttlGen clTxt2 fs05">Resize and Crop Image</h1>\n';
code += '</div>\n';
code += '<div class="btnIco closeGen" onclick="closepopup()">\n';
code += '<span class="ico icoCloseGen"></span>\n';
code += '</div>\n';
code += '<div class="topBgGen clBg3"></div>\n';
code += '</div>\n';
code += '<div class="bodyGen clBg3">\n';
code += '<div class="bodyOutGen">\n';
code += '<div class="bodyInGen">\n';
code += '<p class="desAddGen clTxt3 fs03">Crop your image by moving or resizing it to fit inside the square window.</p>\n';
code += '<div class="wrpFrmGen">\n';
code += '<div class="frmResCrp clBg4 clBd1">\n';
code += '<div class="itemWrapOuterImage">\n';
code += '<div class="itemWrapInnerImage">\n';
code += '<img class="imgResCrp imgCrpCenter" src="'+link+'" alt="Img">\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '<div class="wrpBtnGen">\n';
code += '<input type="submit" class="btnGen clTxtW cutTxt clBgSp1 fs04" onclick="updateCoords()"  value="OK">\n';
code += '<input type="button" class="btnGen clTxtW cutTxt clBgBt1 fs04" value="Cancel" onclick="closepopup()" >\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>';
if(passboxset)
    {
    
    }else
    {

  $('.ovlPopGen.wrpPopAddPop').html(code);
    }
  
   //  console.log(width);
    // console.log(height);
     
   //  console.log(originalwidth2);
  //   console.log(originalheight2);
 $.post(serviceinfo+'checkresizeupload', { imgmid:imgmidset,width:width,height:height,passboxset:passboxset }, function(data) { 
 
 
    
    
    
    
    if(data.filelink!="")
    {
    
    $('.ovlPopGen.wrpPopAddPop .itemWrapInnerImage').html('<img class="imgResCrp imgCrpCenter" src="'+data.filelink+'" alt="Img">\n');

    //$('.ovlPopGen.wrpPopAddPop .imgResCrp').attr("src",data.filelink+'?t='+Math.floor((Math.random()*100000)+1));
    imgmidset=data.imgmid;
    originalwidth2=parseInt(data.width,10);
    originalheight2=parseInt(data.height,10);
    linkto=data.filelink;
    
    

    }
    
   //     console.log(originalwidth2);
//    console.log(originalheight2);
//    
//    
//    console.log(width);
//    console.log(height);
    
    if(passboxset)
    {
    callbackset(data.filename,linkto,originalwidth2,originalheight2,imgmidset,indexset);
    return true;
    
    }else
    {
    
    $('.ovlPopGen.wrpPopAddPop .imgResCrp').Jcrop({
      aspectRatio: width / height,
      trueSize: [originalwidth2,originalheight2],
      minSize:[width,height]
      },function(){
      //console.log('width => '+width);
    //  var bounds = this.getBounds();
//      boundx = bounds[0];
//      boundy = bounds[1];
      jcrop_api = this;
     var datapoint= getRandom();
     //console.log(datapoint);
      jcrop_api.setSelect(datapoint);
     // $preview.appendTo(jcrop_api.ui.holder);
    
    });
    
    $('.ovlPopGen.wrpPopAddPop').show();
    }
 
 });
    
  
  
  
}

function getRandom() {
             
              var dim = jcrop_api.getBounds();
        
        //console.log(dim);
        
        
        return [
            0,
            0,
            width,
            0
        ];
    };

function closepopup()
{
      $('.ovlPopGen.wrpPopAddPop').html('');
      $('.ovlPopGen.wrpPopAddPop').fadeOut(); 

}

function updateCoords()
{

var scale=jcrop_api.tellSelect();

var heightrange=originalheight2-originalheight3;
var widthrange=originalwidth2-originalwidth3;


loaddata();

if(scale.h==0&&scale.w==0&&scale.x==0&&scale.x2==0&&scale.y==0&&scale.y2==0)
{

alertboxui(" Please Select Your Pic to Crop");
loadclosedata();
}else
{




$.post(serviceinfo+'cropupload', { scale:scale,link:linkto,imgmid:imgmidset,originalheight:baseheight,originalwidth:basewidth }, function(data) {

 if(data.error.response)
             {
              alertbox(data.error.error);
             
             }else
            {

            callbackset(data.filename,data.filelink,data.width,data.height,data.imgmid,indexset);

            }

loadclosedata();

});
}



}

function swicthresize(imgid)
{
    $.post(serviceinfo+'checkimgmid', { imgid:imgid }, function(data) { 
    if(data.error.response)
         {
          alertbox(data.error.error);
         
         }else
    {
    
    callbackset2(data.filename,data.filelink,data.width,data.height,data.imgmid,indexset);
    getpopupboxresize(widthset,heightset,data.filelink,data.width,data.height,callbackset,data.imgmid,passboxset);

    }
    
    });


}
var pagecloud=1;
var fecthnow=1;
function getclouddata(page,fecth)
{
       if(!fecth)
        {
         fecth=1;
         }
    
        fecthnow=fecth;
   

    

     
      $.get(serviceinfo+'clouddata', { page:page,fecth:fecth }, function(data) {     
        pagecloud++;    
            if(data.error.response)
            {
          alertbox(data.error.error);
         
         }
            else
            { 
              if(fecth==1)
              {
                if(data.usergallery)
                {
                
                   $.each(data.usergallery, function(index, value) {
                   
                      if(value.thumb)
                      {
                      //console.log(value);
                  // datastr='<div style="float:left;cursor:pointer" onclick="swicthresize('+value.imgid+')"><img width="100" height="100" src="'+value.thumb.newpath+'"></div>';
                   
                   datastr= '<img class="imgCld" src="'+value.thumb.newpath+'" onclick="swicthresize('+value.imgid+')" alt="Img" width="100" height="100">\n';
                   $("#systemgallery .gallerydatanow").append(datastr); 
                   
                      }
                   
                   });
                   
                   
                    if(data.usergallery.length<12)
                   {
                      $("#systemgallery .btnSeeMore.clTxt3.fs02").hide();
                   
                   }
                
                   loadclosedata()
                }
                else
                {
                
                    $("#systemgallery .btnSeeMore.clTxt3.fs02").hide();
                }
              }else
              {
                if(data.systemgallery)
                {
                
                   $.each(data.systemgallery, function(index, value) {
                   
                   
                  // datastr='<div style="float:left;cursor:pointer" onclick="swicthresize('+value.imgid+')"><img width="100" height="100" src="'+value.thumb.newpath+'"></div>';
                   
                   datastr= '<img class="imgCld" src="'+value.thumb.newpath+'" onclick="swicthresize('+value.imgid+')" alt="Img" width="100" height="100">\n';
                   $("#systemgallery .gallerydatanow").append(datastr); 
                   
                   });
                   
                   if(data.systemgallery.length<12)
                   {
                      $("#systemgallery .btnSeeMore.clTxt3.fs02").hide();
                   
                   }
                
                   loadclosedata()
                }
                 else
                {
                
                    $("#systemgallery .btnSeeMore.clTxt3.fs02").hide();
                }
              
              
              }
                 
            
            }
            
     $('.ovlPopGen.wrpPopAddPop').show();       
            });

}
var indexset;
var widthset;
var heightset;
function loadmorecloud()
{

          getclouddata(pagecloud,fecthnow);
}
function swicthcloud(fecth)
{
pagecloud=1;
$("#systemgallery .btnSeeMore.clTxt3.fs02").show();  
$("#systemgallery .gallerydatanow").html(''); 
$("#systemgallery .spnSrcTg.tgCl").removeClass("clSp2");
$("#systemgallery #spnSrcTg-"+fecth).addClass("clSp2");
getclouddata(pagecloud,fecth);
}
function getpopupcloud(callback,callback2,indexnow,width,height,passbox)
{
      callbackset=callback;
      callbackset2=callback2;
      indexset=indexnow;
      widthset= width;
      heightset=height;
      if(passbox)
{
passboxset=1;

}else
{
passboxset=0;
}
      loaddata();
       var datastr="";
     
     var code = '<div class="popGen posPop" id="systemgallery">\n';
        code += '<div class="topGen clBd2 bdBtm">\n';
        code += '<div class="wrpTtlGen">\n';
        code += '<h1 class="ttlGen clTxt2 fs05">Cloud Images</h1>\n';
        code += '</div>\n';
        code += '<div class="btnIco closeGen" onclick="closepopup()">\n';
        code += '<span class="ico icoCloseGen"></span>\n';
        code += '</div>\n';
        code += '<div class="topBgGen clBg3"></div>\n';
        code += '</div>\n';
        code += '<div class="bodyGen clBg3">\n';
        code += '<div class="bodyOutGen">\n';
        code += '<div class="bodyInCldGen">\n';
        code += '<p class="desAddGen clTxt3 fs03">Should the system images or your images</p>\n';
        code += '<div class="wrpSlctGenSrc">\n';
        code += '<div class="slctGenSrc fs04 clTxt2">\n';
        code += '<span class="spnSrcTg tgCl" id="spnSrcTg-2" onclick="swicthcloud(2)">System Images</span>\n';
        code += '<span class="vtcLn clTxt3">|</span>\n';
        code += '<span class="spnSrcTg tgCl clSp2" id="spnSrcTg-1" onclick="swicthcloud(1)">My Images</span>\n';
        code += '</div>\n';
        code += '<div class="wrpOutSrchBox">\n';
        code += '<div class="wrpSrchBox">\n';
        //code += '<input type="text" class="srchBox clBg4 fs01 clTxt3" placeholder="Search">\n';
        code += '<div class="btnIco srchGen">\n';
        code += '<span class="ico icoSrchGen"></span>\n';
        code += '</div>\n';
        code += '</div>\n';
        code += '</div>\n';
        code += '</div>\n';
        code += '<div class="wrpFrmGen wrpFrmGenImg clBg4 clBd1 bdFull">\n';
        code += '<div class="frmGenImg">\n';
        
        code += '<div class="gallerydatanow">\n';
        code += '</div>\n';
        
        code += '<button class="btnSeeMore clTxt3 fs02" onclick="loadmorecloud()">See More</button>\n';
      
      
   //   var divdata="<div style=\"\"><div id=\"systemgallery\" style=\"width:500\"></div><div id=\"usergallery\" style=\"display:none\"></div></div>";
      
          
    
        code += '</div>\n';
        code += '</div>\n';
        code += '</div>\n';
        code += '</div>\n';
        code += '</div>\n';
        code += '</div>';
        
        $('.ovlPopGen.wrpPopAddPop').html(code);
        getclouddata(1);
         
  
 }

function swichlink()
{

if($("#popuplink").val())
{
//var ext = $("#popuplink").val().split('.').pop().toLowerCase();

if(checktype($("#popuplink").val(),['jpg','jpeg','gif','png']))
{
loaddata();
$.post(serviceinfo+'savelink', { url:$("#popuplink").val() }, function(data) {  
loadclosedata();
if(data.error.response)
         {
          alertbox(data.error.error);
         
         }else
            {
          callbackset2(data.filename,data.filelink,data.width,data.height,data.imgmid,indexset);
          getpopupboxresize(widthset,heightset,data.filelink,data.width,data.height,callbackset,data.imgmid,passboxset);  
            
            }

});

}


}


}
 
function getpopuplink(callback,callback2,indexnow,width,height,passbox)
{

callbackset=callback;
callbackset2=callback2;
indexset=indexnow;
widthset= width;
heightset=height;

if(passbox)
{
passboxset=1;

}else
{
passboxset=0;
}


//var divdata="<div style=\"\"><div id=\"systemgallery\" style=\"width:500\"><input id=\"popuplink\" name=\"popup\"><p><input type=\"button\" value=\"OK\" onclick=\"swichlink()\" ><input type=\"button\" value=\"Cancel\" onclick=\"$.colorbox.close();\" ></p></div></div>";

var code = '<div class="popGen posPop">\n';
code += '<div class="topGen clBd2 bdBtm">\n';
code += '<div class="wrpTtlGen">\n';
code += '<h1 class="ttlGen clTxt2 fs05">Insert Image Link</h1>\n';
code += '</div>\n';
code += '<div class="btnIco closeGen" onclick="closepopup()">\n';
code += '<span class="ico icoCloseGen"></span>\n';
code += '</div>\n';
code += '<div class="topBgGen clBg3"></div>\n';
code += '</div>\n';
code += '<div class="bodyGen clBg3">\n';
code += '<div class="bodyOutGen">\n';
code += '<div class="bodyInGen">\n';
code += '<p class="desAddGen clTxt3 fs03">Insert the URL link of image.</p>\n';
code += '<form class="wrpFrmGen" onsubmit="return false;">\n';
code += '<fieldset class="wrpFrmInpTxt">\n';
code += '<div class="wrpInFrmInpTxt">\n';
code += '<input class="frmInpTxt frmGen clBg4 clTxt3 fs03" id="popuplink" type="text" autofocus required>\n';
code += '</div>\n';
code += '</fieldset>\n';
code += '<div class="wrpBtnGen">\n';
code += '<input type="submit" class="btnGen clTxtW cutTxt clBgSp1 fs04" value="Insert" onclick="swichlink()">\n';
code += '<input type="button" class="btnGen clTxtW cutTxt clBgBt1 fs04" value="Cancel" onclick="closepopup()" >\n';
code += '</div>\n';
code += '</form>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>';

$('.ovlPopGen.wrpPopAddPop').html(code);
$('.ovlPopGen.wrpPopAddPop').show(); 
      


}
function savetaguseralldata()
{
if($('#tagusersave').val())
{
$.post(serviceinfo+'saveusertag',{tag:$('#tagusersave').val()}, function(data) { 
getaddusertagdata();
$('.popInpTag').hide();

if($('input[name="taguser[]"]').val())
{
var tagselected=$('input[name="taguser[]"]').val();

tagselected+=' #'+$('#tagusersave').val();

$('input[name="taguser[]"]').val(tagselected);


}else
{
$('input[name="taguser[]"]').val('#'+$('#tagusersave').val());

}
});
}

}
function selecttagdata(tagdata)
{
if($('input[name="taguser[]"]').val())
{
var tagselected=$('input[name="taguser[]"]').val();

tagselected+=' #'+tagdata;

$('input[name="taguser[]"]').val(tagselected);


}else
{
$('input[name="taguser[]"]').val('#'+tagdata);

}
$('.popInpTag').hide();

}
function deletetagdata(utagid)
{
$.post(serviceinfo+'deleteusertag',{utagid:utagid}, function(data) { 

getaddusertagdata();
});
}
function getaddusertagdata()
{

$.post(serviceinfo+'userchecktag',{fid:$('input[name="fid"]').val()}, function(data) { 
var code ='';
code += '<div class="lstInpAddTag clBd2 bdBtm clearfix">\n';
code += '<input type="text" placeholder="Create New Tags" id="tagusersave" class="inpAddCtnt inpPopTagAdd clBg4 clBd2 fs03 clTxt2">\n';
code += '<input type="button" onclick="savetaguseralldata()" value="Save" class="btnGen clTxtW cutTxt clBgBt1 fs04">\n';
code += '</div>';


code +='<div class="ttlTagList clBd2 clBg3 clTxt3 fs02 cutTxt bdBtm"><span>Recommend Tag</span></div>';

if(data.data2.length)
{
$.each(data.data2, function(index, value) {

code += '<div class="lstInpTag clBd2 bdBtm clearfix">\n';
code += '<span class="txtLstInpTag cutTxt fs03 clTxt2" onclick="selecttagdata(\''+value.tag+'\')">'+value.tag+'</span>\n';

code += '</div>';




});

}


code +='<div class="ttlTagList clBd2 clBg3 clTxt3 fs02 cutTxt bdBtm"><span>Personal Set Tag</span></div>';

if(data.data.length)
{

$.each(data.data, function(index, value) {

code += '<div class="lstInpTag clBd2 bdBtm clearfix">\n';
code += '<span class="txtLstInpTag cutTxt fs03 clTxt2" onclick="selecttagdata(\''+value.tag+'\')">'+value.tag+'</span>\n';
code += '<span class="icoLstInpTag ico icoDelTag" onclick="deletetagdata('+value.utagid+')"></span>\n';
code += '</div>';




});





}

$('.wrpBtnInpTag .popInpTag').html(code);


});




}